﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Model.EntityModel;
using Actor = Model.EntityModel.Actor;
using Person = Model.EntityModel.Person;
using System.Data.SqlClient;

namespace DataAccess
{
    public static class PersonAndActorDataAccess
    {
        public static Person InsertPerson(Person newPerson)
        {
            BrigadaEntities context = new BrigadaEntities();
            context.AddToPerson(newPerson);
            context.SaveChanges();
            return newPerson;
        }

        public static bool PersonAlreadyExists(long ciPerson, string Name, string LastName, string MiddleName)
        {
            BrigadaEntities context = new BrigadaEntities();
            var person = (from p in context.Person
                          where p.CiPassport == ciPerson && p.Name == Name && p.PaternalName==LastName && p.MaternalName==MiddleName
                          select p).SingleOrDefault();
            return (person != null);
        }

        public static string GetActorRTypeByComplaintIdAndPersonId(string IdPerson, long IdComp)
        {
            BrigadaEntities context = new BrigadaEntities();
            var actor = (from a in context.Actor
                          where a.IdPerson == IdPerson && a.IdComplaint == IdComp
                         select a).FirstOrDefault();
            return actor.Type;
        }

        public static Person GetPersonByCi(long ciPerson)
        {
            BrigadaEntities context = new BrigadaEntities();
            var person = (from p in context.Person
                          where p.CiPassport == ciPerson
                          select p).SingleOrDefault();
            return person;
        }

        public static Person GetPersonById(string idPerson)
        {
            BrigadaEntities context = new BrigadaEntities();
            var person = (from p in context.Person
                          where p.Id == idPerson
                          select p).FirstOrDefault();
            return person;
        }

        public static List<ShowablePerson> GetAllShowablePersons()
        {
            BrigadaEntities context = new BrigadaEntities();
            var persons = (from p in context.Person
                           select p).ToList();
            List<ShowablePerson> showablePersons = new List<ShowablePerson>();
            foreach (var person in persons)
            {
                showablePersons.Add(new ShowablePerson{Id = person.Id, FullName = person.Name+" "+person.PaternalName+" "+person.MaternalName, CiPassport = person.CiPassport});
            }
            return showablePersons;
        }

        public static List<Person> SearchPersonsByName(string Name, string LastName, string MiddleName)
        {
            BrigadaEntities context = new BrigadaEntities();
            var persons = (from p in context.Person
                          where p.Name.StartsWith(Name) || p.PaternalName.StartsWith(LastName) || p.MaternalName.StartsWith(MiddleName)
                          select p).ToList();
            return persons;
        }

        public static Person UpdatePerson(long ciPerson, Person newInfo)
        {
            BrigadaEntities context = new BrigadaEntities();
            var person = (from p in context.Person
                          where p.CiPassport == ciPerson
                             select p).Single();
            person.CiPassport = newInfo.CiPassport;
            person.Name = newInfo.Name;
            person.PaternalName = newInfo.PaternalName;
            person.MaternalName = newInfo.MaternalName;
            person.BirthDate = newInfo.BirthDate;
            person.Gender = newInfo.Gender;
            person.Phone = newInfo.Phone;
            person.Address = newInfo.Address;
            person.Zone = newInfo.Zone;
            person.Nationality = newInfo.Nationality;
            person.Ocupation = newInfo.Ocupation;
            person.CivilStatus = newInfo.CivilStatus;
            person.BornArea = newInfo.BornArea;
            person.BornCity = newInfo.BornCity;
            person.LevelOfEducation = newInfo.LevelOfEducation;
            person.ActivityStatus = newInfo.ActivityStatus;
            person.WorkPlace = newInfo.WorkPlace;
            person.RegisterDate = newInfo.RegisterDate;
            context.SaveChanges();
            return person;
        }

        public static bool ActorAlreadyExists(string idPerson, long idComplaint,string type)
        {
            BrigadaEntities context = new BrigadaEntities();
            var actor = (from a in context.Actor
                          where a.IdPerson==idPerson && a.IdComplaint==idComplaint && a.Type == type
                          select a).SingleOrDefault();
            return (actor != null);
        }

        public static Actor InsertActor(Actor newActorInfo)
        {
            BrigadaEntities context = new BrigadaEntities();
            context.AddToActor(newActorInfo);
            context.SaveChanges();
            return newActorInfo;
        }

        public static Actor GetActorByTypeAndComplaintId(string type, long idComplaint, string personId=null)
        {
            BrigadaEntities context = new BrigadaEntities();
            if (personId != null)
            {
                var actor = (from a in context.Actor
                             where a.Type == type && a.IdComplaint == idComplaint && a.IdPerson == personId
                             select a).FirstOrDefault();
                return actor;
            }
            else
            {
                var actor = (from a in context.Actor
                             where a.Type == type && a.IdComplaint == idComplaint
                             select a).FirstOrDefault();
                return actor;
            }
        }

        public static List<long> GetComplaintIdsByActorByTypeAndPersonId(string type, string personId)
        {
            BrigadaEntities context = new BrigadaEntities();
            var actors = (from a in context.Actor
                         where a.IdPerson == personId && a.Type == type
                         select a).ToList();
            List<long> ids = new List<long>();
            foreach (var a in actors)
            {
                ids.Add(a.IdComplaint);
            }
            return ids;
        }

        public static bool IsRelapse(long victimCi,string victimName, string victimLastName, string victimMiddleName, long aggressorCi, string aggressorName, string aggressorLastName, string aggressorMiddleName)
        {
            BrigadaEntities context = new BrigadaEntities();
            var vict = GetPersonByCi(victimCi);
            var aggress = GetPersonByCi(aggressorCi);
            if (vict != null && aggress != null)
            {
                var resVic = (from a in context.Actor
                              where
                                  a.IdPerson == vict.Id && a.Type == "Victima" && a.Person.Name == victimName &&
                                  a.Person.PaternalName == victimLastName && a.Person.MaternalName == victimMiddleName
                              select a.IdComplaint).ToList();
                var resAgg = (from a in context.Actor
                              where
                                  a.IdPerson == aggress.Id && a.Type == "Agresor" && a.Person.Name == aggressorName &&
                                  a.Person.PaternalName == aggressorLastName &&
                                  a.Person.MaternalName == aggressorMiddleName
                              select a.IdComplaint).ToList();
                if (resVic != null && resAgg != null)
                {
                    foreach (var l in resVic)
                    {
                        if (resAgg.Contains(l))
                            return true;
                    }
                }
            }
            return false;
        }


        public static void UpdatePersonWorkPlace(string workplace, long ciPerson)
        {
            BrigadaEntities context = new BrigadaEntities();
            var person = (from p in context.Person
                          where p.CiPassport == ciPerson
                          select p).SingleOrDefault();
            person.WorkPlace = workplace;
            context.SaveChanges();
        }
    }
}
